<?php
/**
 * Created by PhpStorm.
 * User: Dave
 * Date: 2018/4/4
 * Time: 11:18
 */

namespace Biz\model;


class ShopCarouselBanner extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_shop';

    const _DATABASE_ = 'shop';

    const _TABLE_NAME = 'shop_carousel_banner';

    // 状态为正常
    const STATUS_NORMAL = 1;
    // 状态为被删除
    const STATUS_DELETE = 9;

    // 所有人可见
    const STATUS_VISIBLE = 1;
    // N小时内注册可见
    const WIKI_STATUS_INVISIBLE = 2;
    // 新用户可见
    const NEW_USER_VISIBLE = 3;

    // banner最大显示数量
    const MAX_COUNT = 10;

    // 是否自动抓取
    const IS_GRAB = 1; // 自动抓取
    const NO_GRAB = 2; // 手动填写

    // 首页商品显示行数
    const STYLE_ONE = 1; // 一行
    const STYLE_TWO = 2; // 两行

    // banner类型
    const TYPE_BANNER = 1022; // 顶部banner
    const TYPE_ONE = 1010; //  横条配置模块
    const TYPE_THREE = 1011; // 三分配置模块
    const TYPE_FOUR = 1009; // 四分配置模块
    const TYPE_SECOND_CATE = 1023; //跳转二级类目banner
    const TYPE_MEAL = 1024; // 建材频道页
    const TYPE_FIVE_POSITION = 1025; //五分位
    const TYPE_TWO_ONELINE = 1026; // 一行两个
    const TYPE_SUMITOMO_IS_BUYING = 1027;  //住友都在买
//    const TYPE_SEARCH_RESULTS = 1028; //大搜索结果页
    const TYPE_ORDER_CART = 1200;//购物车banner
    const TYPE_ORDER_CONFIRM = 1201;//确认订单页
    const TYPE_ORDER_LIST = 1203;//我的订单页
    const TYPE_CART_SUSPENSION = 1204; // 购物车悬浮
    const TYPE_FLASH_MODULE = 1205; // 限时购模块

    const TYPE_GOODS_LIST = 1029; // 商品模块
    const TYPE_THREE_TITLE = 1030; // 带标题三分位
    const TYPE_PINTUAN = 1031; // 拼团

    const TYPE_FLASH_SALE = 1206; // 限时购页面

    const TYPE_APP_BANNER_INFO =  1028;
    const TYPE_APP_BANNER_INFO_MAP = [
        self::TYPE_SUMITOMO_IS_BUYING,
//        self::TYPE_SEARCH_RESULTS
    ];

    public static $bannerType = [
        self::TYPE_BANNER => '顶部banner',
        self::TYPE_ONE => '横条配置模块',
        self::TYPE_THREE => '三分配置模块',
        self::TYPE_FOUR => '四分配置模块',
        self::TYPE_FLASH_MODULE => '限时购模块',
        self::TYPE_MEAL => '建材频道页',
        self::TYPE_SECOND_CATE => '二级类目页',
        self::TYPE_ORDER_CART => '购物车',
        self::TYPE_ORDER_CONFIRM => '确认订单页',
        self::TYPE_ORDER_LIST => '我的订单页',
        self::TYPE_FIVE_POSITION => '五分位',
        self::TYPE_TWO_ONELINE => '一行两个',
        self::TYPE_SUMITOMO_IS_BUYING => '住友都在买【小专题】',
//        self::TYPE_SEARCH_RESULTS => '大搜索结果【商品tab】',
        self::TYPE_CART_SUSPENSION => '购物车悬浮',
        self::TYPE_FLASH_SALE => '限时购页面',
        self::TYPE_GOODS_LIST => '商品模块',
        self::TYPE_THREE_TITLE => '带标题三分位',
        self::TYPE_PINTUAN => '拼团',
    ];

    public static function getCarouselBannerList($time, $count)
    {
        return self::select(['id', 'banner', 'link', 'link_dest', 'link_type', 'visible_user', 'show_type', 'type', 'sort', 'commend'], [
            'AND' => [
                'start_time[<]' => $time,
                'end_time[>=]' => $time,
                'status' => self::STATUS_NORMAL,
                'visible_user' => self::STATUS_VISIBLE,
                'type' => self::TYPE_BANNER,
            ],
            'ORDER' => ['sort' => 'DESC'],
            'LIMIT' => $count,
        ]);
    }

    public static function getCarouselBannerByVisible($time, $visible)
    {
        return self::select(['id', 'banner', 'link', 'link_dest', 'link_type', 'visible_user', 'visible_time', 'show_type', 'type', 'sort', 'commend'], [
            'AND' => [
                'start_time[<]' => $time,
                'end_time[>=]' => $time,
                'status' => self::STATUS_NORMAL,
                'visible_user' => $visible,
                'type' => self::TYPE_BANNER,
            ],
            'ORDER' => ['sort' => 'DESC'],
            'LIMIT' => self::MAX_COUNT,
        ]);
    }

    public static function getBannerInfoById($id)
    {
        return self::get('*', [
            'AND' => [
                'id' => $id,
                'status' => self::STATUS_NORMAL
            ]
        ]);
    }

    public static function addBanner($data)
    {

        $data['status'] = self::STATUS_NORMAL;
        $data['addtime'] = date('Y-m-d H:i:s');
        if (self::insert($data)->rowcount()) {

            return self::id();
        }
    }

    public static function updateBannerInfoById($id, $data)
    {

        $data['edittime'] = date('Y-m-d H:i:s');
        if (self::update($data, ['id' => $id])->rowcount()) {

            return $id;
        }
    }

    public static function getBannerList($data, $limit)
    {
        $where = array_filter([
            'type' => $data['type'],
            'status' => self::STATUS_NORMAL,
            'show_type' => $data['show_type'],
            'start_time[<=]' => $data['start_time'],
            'end_time[>=]' => $data['end_time'],
        ]);

        return self::select('*', [
            'AND' => $where,
            'ORDER' => ['start_time' => 'DESC'],
            'LIMIT' => $limit

        ]);
    }

    public static function getBannerCount($data)
    {
        $where = array_filter([
            'type' => $data['type'],
            'status' => self::STATUS_NORMAL,
            'show_type' => $data['show_type'],
            'start_time[<=]' => $data['start_time'],
            'end_time[>=]' => $data['end_time'],
        ]);

        return self::count('*', [
            'AND' => $where,
        ]);
    }

    public static function delBannerById($id)
    {

        if (self::update(['status' => self::STATUS_DELETE], ['id' => $id])->rowcount()) {

            return $id;
        }
    }

    public static function checkWeight($weight, $where)
    {
        $where['AND']['sort'] = $weight;
        $where['AND']['status'] = self::STATUS_NORMAL;

        return self::select('sort', $where);
    }

    public static function getBannerPage($where)
    {

        return self::select('id', [
            'AND' => [
                'type' => $where['type'],
                'status' => self::STATUS_NORMAL,
            ],
            'ORDER' => ['start_time' => 'DESC'],
        ]);
    }

    //  缓存数据
    public static function getMultipleBanner($time)
    {
        return self::select('*', [
            'AND' => [
                'start_time[<=]' => $time,
                'end_time[>=]' => $time,
                'status' => self::STATUS_NORMAL,
                'type' => [
                    self::TYPE_ONE, self::TYPE_THREE, self::TYPE_FOUR,
                    self::TYPE_FIVE_POSITION, self::TYPE_TWO_ONELINE, self::TYPE_SUMITOMO_IS_BUYING,
                    self::TYPE_CART_SUSPENSION, self::TYPE_FLASH_MODULE,
                    self::TYPE_FLASH_SALE, self::TYPE_GOODS_LIST,
                    self::TYPE_THREE_TITLE,self::TYPE_PINTUAN
                ],
            ],
            'ORDER' => ['sort' => 'DESC'],
        ]);
    }

    public static function getMultiSecondClassifyBannerInfos($cateIds, $timestamp)
    {
        $bannerInfos = self::select('*', [
            'start_time[<]' => $timestamp,
            'end_time[>=]' => $timestamp,
            'show_dest_id' => $cateIds,
            'type' => self::TYPE_SECOND_CATE,
            'status' => self::STATUS_NORMAL,
            'ORDER' => ['sort' => 'DESC']
        ]);
        $formats = [];
        foreach ($bannerInfos as $bannerInfo) {
            if (!isset($formats[$bannerInfo['show_dest_id']])) {
                $formats[$bannerInfo['show_dest_id']] = $bannerInfo;
            }
        }
        return $formats;
    }

    public static function getBannerByType($type, $showType = 0)
    {
        $time = date('Y-m-d H:i:s');
        $where = [
            'start_time[<=]' => $time,
            'end_time[>=]' => $time,
            'status' => self::STATUS_NORMAL,
            'type' => $type,
        ];

        if ($showType > 0) {
            $where['show_type'] = $showType;
        }
        return self::select(['id', 'banner', 'link', 'link_dest', 'link_type', 'type', 'title', 'banner_desc', 'sort', 'show_type', 'start_time', 'end_time'], [
            'AND' => $where,
            'ORDER' => ['sort' => 'DESC'],
        ]);
    }

}